package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public BedVo getBedById(Long id) {
        Bed bed = bedMapper.getBedById(id);
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bed, bedVo);
        return bedVo;
    }

    @Override
    public void add(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        try {
            bedMapper.add(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void update(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        bedMapper.update(bed);
    }

    @Override
    public void delete(Long id) {
        bedMapper.delete(id);
    }
}

